package com.vinoxm.rss.service;

import com.vinoxm.rss.model.RssResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Log4j2
public class RssResultService {

    private final String[] matches = {
            ".*?\\[([0-9]{1,4}(\\.5)?)(v[0-3])?\\].*",
            ".*?- ([0-9]{1,4}(\\.5)?)(v[0-3])? .*",
            ".*?第([0-9]{1,4}(\\.5)?)(v[0-3])?(集|话)(v[0-3])?.*",
            ".*?【([0-9]{1,4}(\\.5)?)(v[0-3])?】.*",
//            ".* ([0-9]{1,4}(\\.5)?)(v[0-3])? .*"
    };

    public void handleRssResultEpisode(RssResult rssResult) {
        if (rssResult.getEpisode() == null) {
            rssResult.setEpisode("-");
            String title = rssResult.getTitle();
            for (String match : matches) {
                if (Pattern.matches(match, title)) {
                    Matcher matcher = Pattern.compile(match).matcher(title);
                    if (matcher.find()) {
                        String episode = matcher.group(1);
                        rssResult.setEpisode(episode);
                        break;
                    }
                }
            }
        }
    }
}
